/* 

THIS DOFILE PRODUCES SEVERAL FIGURES USING THE MAIN AGENCY X MONTH-LEVEL DATASET FOR: 

"Regulation by Reputation? Intermediaries, Labor Abuses, and International Migration" 
- A. Nilesh Fernando (University of Notre Dame); corresponding author (nilesh.fernando@nd.edu)  
- Niharika Singh (University of Notre Dame) 

These include Figure 1, Figure 2, Appendix A9, Appendix A10,  Appendix B5, and Appendix C2 

See readme.docx for details 

*/ 


clear all
set matsize 1000
set more off 
set graphics off
#delimit;



cd "~/Desktop/migrec_replication/do/";


use ../dta_secure/5_firm_mn_final.dta, replace;


*------------------------------------------------------*
* FIGURE 1 AND 2: EVENT STUDY PLOTS  (PANEL A AND C)   *
*------------------------------------------------------;

preserve;

*rename vars so more tractable;

rename num_job_orders_win job_orders;
rename jo_quality contractq ;
rename mean_salary_d_win salary; 
rename renew_emp_or_self_share renew; 


foreach x of varlist job_orders contractq  {; 


** Figure 1: all agencies; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & year >= 2008, r; 

estimates store `x'_all_qrt_08; 


coefplot (`x'_all_qrt_08, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%15)
 lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2008" 5 "2009" 9 "2010" 13 "2011" 17 "2012" 21
   "2013" 25 "2015")
   xline(8.5, lcolor(gs0) lpattern(solid) lwidth(thick)) xline(16.5, lcolor(black%45)
    lpattern(dash) lwidth(thick)) graphregion(color(white)) ylabel(,nogrid) base omitted;

graph export ../out/`x'_all_qrt_08.pdf, replace; 


** Figure 2: low only; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & low_score == 1 & year >= 2008, r; 

estimates store `x'_low_qrt_08; 


coefplot (`x'_low_qrt_08, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%15)
 lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2008" 5 "2009" 9 "2010" 13 "2011" 17 "2012" 21 "2013" 25 "2015")
   xline(8.5, lcolor(gs0) lpattern(solid) lwidth(thick)) xline(16.5, lcolor(black%45)
    lpattern(dash) lwidth(thick)) graphregion(color(white)) ylabel(,nogrid) base omitted;
   

graph export ../out/`x'_low_qrt_08.pdf, replace; 


};


*-------------------------------------------------*
* FIGURE 1 AND 2: EVENT STUDY PLOTS  (PANEL B)    *
*-------------------------------------------------;

** SECOND, GENERATE PANEL B IN FIGURE 1 AND 2; 

*** migrant-level data; 


foreach x of varlist salary {; 


*** AFTER 2008 only; 

** all agencies; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & year >= 2008, r; 

estimates store `x'_all_qrt_08; 


coefplot (`x'_all_qrt_08, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%15) 
lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2008" 5 "2009" 9 "2010" 13 "2011" 17 "2012" 21
   "2013" 25 "2014" 29 "2015")
   xline(8.5, lcolor(gs0) lpattern(solid) lwidth(thick)) xline(16.5, lcolor(black%45)
    lpattern(dash) lwidth(thick)) graphregion(color(white)) ylabel(,nogrid) base omitted;

graph export ../out/`x'_all_qrt_08.pdf, replace; 


** low only; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & low_score == 1 & year >= 2008, r; 

estimates store `x'_low_qrt_08; 


coefplot (`x'_low_qrt_08, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%15)
 lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2008" 5 "2009" 9 "2010" 13 "2011" 17 "2012" 21 "2013" 25 "2014" 29 "2015")
   xline(8.5, lcolor(gs0) lpattern(solid) lwidth(thick)) xline(16.5, lcolor(black%45)
    lpattern(dash) lwidth(thick)) graphregion(color(white)) ylabel(,nogrid) base omitted;

graph export ../out/`x'_low_qrt_08.pdf, replace; 
};

restore;

*------------------------------------------------------*
* APPENDIX A9: DISTRIBUTION OF RATINGS                 *
*------------------------------------------------------;


* Panel A: All Eligible Agencies; 

histogram performance_rate if elig_firm == 1 & year == 2009 & month == 1, color(blue%20) width(1) start(0)
fi(50) xline(20) xline(35) xline(50) xline(60) xtitle("Performance Rating, n=307", axis(1) size(small)) 
 xtitle("", axis(2) size(small))  
xaxis(1 2) xlabel(0(5)70, labsize(vsmall) axis(1))
 xlabel(10 "0 stars: < 19.9  " 27.5 "1 star: 20-34.9"  42.5 "2 stars: 35-49.9" 55 "3 stars: 50-59.9" 
 65 "4 stars: 60-79.9", labsize(vsmall) axis(2) notick)  freq;

graph export "../out/rating_dist_all.pdf", replace;



* Panel B: Recruited 100-155 Migrants in 2009;


histogram performance_rate if elig_firm == 1 & year == 2009 & month == 1 & comp_opt == 1, color(blue%20)
 width(1) start(0)
fi(50) xline(20) xline(35) xline(50) xline(60) xtitle("Performance Rating, n=78", axis(1) size(small)) 
 xtitle("", axis(2) size(small)) 
xaxis(1 2) xlabel(0(5)70, labsize(vsmall) axis(1)) xlabel(10 "0 stars: < 19.9  " 27.5 "1 star: 20-34.9"  
42.5 "2 stars: 35-49.9" 55 "3 stars: 50-59.9" 65 "4 stars: 60-79.9", labsize(vsmall) axis(2) notick)  freq;

graph export "../out/rating_dist_comp_opt.pdf", replace;


*------------------------------------------------------*
* APPENDIX A10: QUARTERLY VARIATION IN RECRUITMENT     *
*------------------------------------------------------;

preserve; 


keep if comp_opt2 ==1 &  qrtr >= 15 & qrtr <= 20;  

collapse (sum) migrant (first) elig_firm , by(agency_id qrtr); 

* create labels; 

egen ag_id  = group(agency_id); 
gen agg_id = ag_id; 
tostring ag_id , replace; 

replace ag_id = "" if agg_id > 20; 


twoway (scatter migrant qrtr if elig_firm == 1  & migrant <= 100, jitter(10)  
color(red%50) fi(inten30) ) (scatter migrant qrtr if elig_firm == 0 & migrant <= 100, 
jitter(10)  color(blue%50) fi(inten30)) , legend(lab(1 "Eligible") lab(2 "Comparison"))
xlabel(15 "3Q-08" 16 "4Q-08" 17 "1Q-09" 18 "2Q-09" 19 "3Q-09" 20 "4Q-09")
xscale(range(14.5(1)20.5)) ytitle("Quarterly Migrant Recruitment") xtitle("") ;
   
   
graph export "../out/stochastic_recruitment.pdf", replace;

  
restore; 


*------------------------------------------------------*
* APPENDIX B5: RAW TRENDS						       *
*------------------------------------------------------;

		
* PANEL A: Migrant Recruitment;


binscatter migrant_t975 qrtr if comp_opt == 1  & migrant != 0 ,  
by(elig_firm) line(connect) xline(21, lcolor(blue))  xline(29, lcolor(green))  ytitle("Migrants")
discrete xtitle("Year") legend(order(2 1) rows(1) lab(1 "Comparison") lab(2 "Eligibles") 
size(small)) xlabel(1 "2005" 5 "2006" 9 "2007" 13 "2008" 17 "2009" 21 "2010" 25 "2011" 29
"2012" 33 "2013" 37 "2014" 41 "2015") savegraph(../out/migrant_trend.pdf) replace;


* PANEL B: Job Orders;

binscatter num_job_orders qrtr if comp_opt == 1   /* & nojo != 1*/ , 
by(elig_firm) line(connect) xline(21, lcolor(blue))  xline(29, lcolor(green))  ytitle("Job Orders")
discrete xtitle("Year") legend(order(2 1) rows(1) lab(1 "Comparison") lab(2 "Eligibles") 
size(small)) xlabel(5 "2006" 9 "2007" 13 "2008" 17 "2009" 21 "2010" 25 "2011" 29
"2012" 33 "2013" 37 "2014" 41 "2015") savegraph(../out/joborder_trend.pdf) replace;


* PANEL C: Complaint Intensity;

binscatter comp_int qrtr if comp_opt == 1  , by(elig_firm) line(connect) xline(21, lcolor(blue))
  xline(29, lcolor(green))  ytitle("Complaint Intensity")
discrete xtitle("Year") legend(order(2 1) rows(1) lab(1 "Comparison") lab(2 "Eligibles") 
size(small)) xlabel(1 "2005" 5 "2006" 9 "2007" 13 "2008" 17 "2009" 21 "2010" 25 "2011" 29
"2012" 33 "2013" 37 "2014" 41 "2015") savegraph(../out/complaint_trend.pdf) replace;


*-------------------------------------------*
* APPENDIX C2: PLOTS OF LAGGED EFFECTS      *
*-------------------------------------------;


preserve;

*rename vars so more tractable;

rename num_job_orders_win job_orders;
rename jo_quality contractq ;
rename mean_salary_d_win salary; 
rename renew_emp_or_self_share renew; 

drop if year == 2005; 


*** job order-level data; 

rename num_jo_good_emp_pre2009_25 good_orders;


foreach x of varlist job_orders good_orders contractq   {; 


** QUARTERLY; 
** all agencies; 
* lags; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & qrtr < 21, cluster(agency_id); 

estimates store `x'_all_qrt; 


coefplot (`x'_all_qrt, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%30) 
lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2006" 5 "2007" 9 "2008" 13 "2009" 17 "2010") 
  ylabel(,nogrid) base omitted;

graph export ../out/`x'_lag_qrt.pdf, replace; 


};

*** migrant-level data; 

foreach x of varlist migrant  salary renew {; 


** QUARTERLY; 
** all agencies; 
* lags; 

reg `x' i.elig_firm##ib20.qrtr i.agency_id 
if comp_opt == 1 & qrtr < 21, cluster(agency_id); 

estimates store `x'_all_qrt; 


coefplot (`x'_all_qrt, msymbol(s) mlcolor(black) mfcolor(black) ciopts(lcolor(gs%30) lpattern(dash) recast(rcap))), yline(0, lcolor(black%65) lpattern(dot)) 
  keep(1.elig_firm#*.qrtr) vertical xtitle("Year") 
  ytitle("Estimated Effect") xlabel(1 "2006" 5 "2007" 9 "2008" 13 "2009" 17 "2010") ylabel(,nogrid) base omitted;

graph export ../out/`x'_lag_qrt.pdf, replace; 

}; 

restore;



